import { http } from '../utils/http/index'
import type { UploadSignResponse } from '../models/Upload'
import type { LoginResponse } from '../models/LoginModel'

// 修改个人信息请求体
export interface ModifyUserRequest {
  userId?: string
  sex?: number
  nickName?: string
  realName?: string
  birthday?: string
  email?: string
  startWorkDate?: string
  graduationYear?: number
  position?: string
}

/**
 * 修改用户信息
 */
export async function modifyUserInfo(data: ModifyUserRequest): Promise<void> {
  await http.post('/portal/user/modify', data)
}

/**
 * 获取头像上传签名
 */
export async function getAvatarUploadSign(): Promise<UploadSignResponse> {
  const response = await http.get('/portal/user/get_avatar_upload_sign')
  return response as UploadSignResponse
}

/**
 * 更新用户头像
 */
export async function updateAvatar(avatarUrl: string): Promise<void> {
  await http.post(`/portal/user/avatar_update?avatarUrl=${encodeURIComponent(avatarUrl)}`)
}

// HR 申请请求体
export interface HrApplyRequest {
  companyId: string
  code: string
  realName: string
  position: string
}

/**
 * HR 申请
 */
export async function hrApply(data: HrApplyRequest): Promise<void> {
  await http.post('/portal/user/hr_apply', data)
}

/**
 * 切换登录角色
 * @param role 1-切换到求职者 2-切换到招聘者
 */
export async function changeLoginRole(role: number): Promise<LoginResponse> {
  const response = await http.post(`/portal/user/change_login_role?role=${role}`)
  return response as LoginResponse
}

// 附件信息
export interface AttachmentInfo {
  id: number
  userId: string
  fileName: string
  fileSizeMb: number
  fileType: string
  fileUrl: string
  updateTime: string
}

/**
 * 获取附件简历列表
 */
export async function getAttachments(): Promise<AttachmentInfo[]> {
  const response = await http.get('/portal/user/get_attachments')
  return response as AttachmentInfo[]
}

/**
 * 获取附件上传签名
 * @param fileExt 文件扩展名（如 pdf, doc, docx）
 */
export async function getAttachmentUploadSign(fileExt: string): Promise<UploadSignResponse> {
  const response = await http.post('/portal/user/get_attachment_upload_sign', { fileExt })
  return response as UploadSignResponse
}

// 添加附件请求体
export interface AddAttachmentRequest {
  fileName: string
  fileSizeMb: number
  fileType: string
  fileUrl: string
}

/**
 * 添加附件简历
 */
export async function addAttachment(data: AddAttachmentRequest): Promise<void> {
  await http.post('/portal/user/add_attachment', data)
}

/**
 * 删除附件简历
 * @param attachmentId 附件ID
 */
export async function deleteAttachment(attachmentId: number): Promise<void> {
  await http.post(`/portal/user/delete_attachment?attachmentId=${attachmentId}`)
}
